const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
  entry: './src/main.js', // 入口文件
  output: {
    filename: 'bundle.js', // 输出文件名
    path: path.join(__dirname, 'dist') // 输出目录
  },
  module: {
    rules: [
      // 处理vue文件
      {
        test: /\.vue$/,
        use: 'vue-loader'
      },
      // 处理js文件
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'] //使用插件集合
          }
        }
      },
      // 处理less文件
      {
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        test: /\.css$/,
        use: ['vue-style-loader', 'css-loader']
      },
      // 处理其他资源文件
      {
        test: /\.(png|svg|gif|jpe?g)$/,
        use: {
          loader: 'url-loader',
          options: {
            esModule: false,
            limit: 10 * 1024,
            name: 'img/[name].[contenthash:8].[ext]' // 指定输出路径
          }
        }
      },
      // eslint配置
      {
        test: /.js|vue$/,
        exclude: /node_modules/,
        use: 'eslint-loader',
        enforce: 'pre'
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      BASE_URL: '"vue"'
    }),
    new HtmlWebpackPlugin({
      title: 'webpack vue',
      template: './public/index.html'
    }),
    new VueLoaderPlugin()
  ]
}
